package com.tang.lts.core.nio.loop;

import com.tang.lts.core.nio.processor.NioProcessor;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Tang
 * @classname FixedNioSelectorLoopPool
 * @description [  ]
 * @date 2022/5/16 14:06
 */
public class FixedNioSelectorLoopPool implements NioSelectorLoopPool {

    private String name;
    private static AtomicInteger id = new AtomicInteger(0);

    private NioSelectorLoop[] loops;
    private NioProcessor nioProcessor;

    public FixedNioSelectorLoopPool(int size, String name, NioProcessor nioProcessor) {
        this.name = name;
        this.nioProcessor = nioProcessor;

        loops = new NioSelectorLoop[size];
        for (int i = 0; i < size; i++) {
            loops[i] = new NioSelectorLoop(name + "-" + id.getAndIncrement(), nioProcessor);
        }
    }

    @Override
    public NioSelectorLoop getNioSelectorLoop() {
        return loops[id.getAndIncrement() % loops.length];
    }
}